package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.StaffArrangementMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.StaffArrangement;
import edu.hubu.talentmanagementsystem.service.IStaffArrangementService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class StaffArrangementServiceImpl extends ServiceImpl<StaffArrangementMapper, StaffArrangement> implements IStaffArrangementService {

    private final MapperContext context;

    public StaffArrangementServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getStaffArrangementMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<StaffArrangement> insert(@EncodeParam StaffArrangement record) {
        int insert = context.getStaffArrangementMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getStaffArrangementMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<StaffArrangement> selectById(Integer id) {
        QueryWrapper<StaffArrangement> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<StaffArrangement> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(StaffArrangement::getStaffArrangementId, id);
        return Optional.of(context.getStaffArrangementMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<StaffArrangement> selectAll(String order) {
        QueryWrapper<StaffArrangement> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<StaffArrangement> select = getColumnWrapper(wrapper.lambda());
            return context.getStaffArrangementMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<StaffArrangement> select = getColumnWrapper(wrapper.lambda());
        return context.getStaffArrangementMapper().selectList(select);
    }

    @Override
    public Page<StaffArrangement> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<StaffArrangement> wrapper = new QueryWrapper<>();
        Page<StaffArrangement> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<StaffArrangement> select = getColumnWrapper(wrapper.lambda());
            return context.getStaffArrangementMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<StaffArrangement> select = getColumnWrapper(wrapper.lambda());
        return context.getStaffArrangementMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<StaffArrangement> getColumnWrapper(LambdaQueryWrapper<StaffArrangement> wrapper) {
        return wrapper.select(
                StaffArrangement::getResource
                    ,
                StaffArrangement::getDepartment
                    ,
                StaffArrangement::getArrangeStaff
                    ,
                StaffArrangement::getStaffArrangementId
                    ,
                StaffArrangement::getArrangeId
                    ,
                StaffArrangement::getStaffArrangementDepartmentId
                    ,
                StaffArrangement::getStaffArrangementResourceId
        );
    }

    @Override
    public Page<StaffArrangement> selectLikePaged( Integer currentPage,
             Integer pageSize, StaffArrangement record, String order) {
        Page<StaffArrangement> page = new Page<>(currentPage, pageSize);
        QueryWrapper<StaffArrangement> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getStaffArrangementMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getStaffArrangementMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<StaffArrangement> getQueryWrapper(QueryWrapper<StaffArrangement>wrapper, StaffArrangement record) {
        LambdaQueryWrapper<StaffArrangement> lambda = wrapper.lambda();
        if (record.getResource() != null) {
            lambda = lambda.eq(StaffArrangement::getResource,record.getResource());
        }
        if (record.getDepartment() != null) {
            lambda = lambda.eq(StaffArrangement::getDepartment,record.getDepartment());
        }
        if (record.getArrangeStaff() != null) {
            lambda = lambda.eq(StaffArrangement::getArrangeStaff,record.getArrangeStaff());
        }
        if (record.getStaffArrangementId() != null) {
            lambda = lambda.eq(StaffArrangement::getStaffArrangementId,record.getStaffArrangementId());
        }
        if (record.getArrangeId() != null) {
            lambda = lambda.eq(StaffArrangement::getArrangeId,record.getArrangeId());
        }
        if (record.getStaffArrangementDepartmentId() != null) {
            lambda = lambda.eq(StaffArrangement::getStaffArrangementDepartmentId,record.getStaffArrangementDepartmentId());
        }
        if (record.getStaffArrangementResourceId() != null) {
            lambda = lambda.eq(StaffArrangement::getStaffArrangementResourceId,record.getStaffArrangementResourceId());
        }
        return lambda;
    }

    public int count(StaffArrangement record) {
        return context.getStaffArrangementMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<StaffArrangement> updateById(Integer id, @EncodeParam StaffArrangement record) {
        record.setStaffArrangementId(id);
        int update = context.getStaffArrangementMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<StaffArrangement> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<StaffArrangement> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(StaffArrangement::getStaffArrangementId, id);
            return Optional.of(context.getStaffArrangementMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
